/**
 * related to memberList.ui
 * 
 * @Author : woon
 * @Timestamp : 2016-08-04
 */
var nf = sm("do_Notification");
var page = sm("do_Page");
var root = ui("$");
var btnSetManager = ui("btnSetManager");
var btnKick = ui("btnKick");
var btnHide =  ui("btnHide");
var circleUrl = require("url/circle");
var qqHttp = require("helper/http");

root.setMapping({
	"alayoutRoot.tag" : "circleId",//圈子id
	"btnSetManager.tag" : "type",//成员类型1-圈主;2-管理员;3-普通成员',
	"lableMember.text" : "memberId", //成员id
	"btnHide.tag" : "isBlack" //是否已屏蔽:1-是;2-否;
});

root.on("dataRefreshed", function(){
	resetBtnStatus();
});

//设置管理员
btnSetManager.on("touch",function(){
	var circleId = ui("alayoutRoot").tag;//圈子id
	var memberId = ui("lableMember").text;//成员id
	var type = ui("btnSetManager").tag;//成员类型
	
	if(type=='2'){//已经是管理员
		qqHttp.post(circleUrl.url.member.cancleCircleManager, {memberId:memberId,circleId:circleId}, function(data) {
			ui("btnSetManager").tag = 3;//变成普通成员
			resetBtnStatus();
			nf.toast("取消管理员成功");
			page.fire("reflash_member");
		});
	}else if(type=='3'){
		qqHttp.post(circleUrl.url.member.setCircleManager, {memberId:memberId,circleId:circleId}, function(data) {
			ui("btnSetManager").tag = 2;//变成管理员
			resetBtnStatus();
			nf.toast("设置管理员成功");
			page.fire("reflash_member");
		});
	}
})

function resetBtnStatus(){
	var type = ui("btnSetManager").tag;
	if(type=='1'){//圈主
		btnSetManager.hide();
	}else if(type=='2'){//已经是管理员
		btnSetManager.text = "取消管理员";
		btnSetManager.bgColor="C0C0C0FF";
	}else if(type=='3'){
		btnSetManager.text = "设为管理员";
		btnSetManager.bgColor="FF7E00FF";
	}
}

function resetHideBtnStatus(){
	var isBlack = ui("btnHide").tag;
	if(isBlack=='1'){//已经是屏蔽
		btnHide.text = "取消屏蔽";
		btnHide.bgColor="C4C9D0FF";
	}else if(isBlack=='2'){
		btnHide.text = "屏蔽";
		btnHide.bgColor="FF80FFFF";
	}
}

//屏蔽、取消屏蔽
btnHide.on("touch",function(){
	var circleId = ui("alayoutRoot").tag;//圈子id
	var memberId = ui("lableMember").text;//成员id
	var isBlack = ui("btnHide").tag;//是否已屏蔽：1-是;2-否;
	
	if(isBlack=='2'){//未屏蔽
		nf.confirm("该成员被屏蔽后不能发布动态,是否继续操作?", "", "继续", "取消", function(data, e) {
			if (data == 1) {
				qqHttp.post(circleUrl.url.member.setBlack, {memberId:memberId,circleId:circleId}, function(data) {
					ui("btnHide").tag = 1;//变成-屏蔽
					resetHideBtnStatus();
					nf.toast("屏蔽成功");
				});
			}
		})
	}else if(isBlack=='1'){//已屏蔽
		qqHttp.post(circleUrl.url.member.unSetBlack, {memberId:memberId,circleId:circleId}, function(data) {
			ui("btnHide").tag = 2;//变成-未屏蔽
			resetHideBtnStatus();
			nf.toast("取消屏蔽成功");
		});
	}
})

//踢走成员
btnKick.on("touch",function(){
	var circleId = ui("alayoutRoot").tag;//圈子id
	var memberId = ui("lableMember").text;//成员id
	var type = ui("btnSetManager").tag;//成员类型
	
	if(type=='1'){//圈主
		nf.toast("不能踢走圈主");
	}else{
		nf.confirm("你确定要踢走该成员吗?", "", "确定", "取消", function(data, e) {
			if (data == 1) {
				qqHttp.post(circleUrl.url.member.kickMember, {memberId:memberId,circleId:circleId}, function(data) {
					nf.toast("踢人成功");
					page.fire("kick_member");
				});
			}
		})
		
	}
})